package com.example.mylock;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * description: DeadLockdemo01 <br>
 * date: 2021/2/26 16:08 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class DeadLockdemo01 {

    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void main(String[] args) {
        /*new Thread(()->{
            synchronized (obj1){
                System.out.println("这里获取了锁1");
                try {
                    Thread.sleep(300L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj2){
                    System.out.println("这里获取了锁2");
                }
            }

        }).start();
        new Thread(()->{
            synchronized (obj2){
                System.out.println("这里获取了锁2");
                try {
                    Thread.sleep(300L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj1){
                    System.out.println("这里获取了锁1");
                }
            }
        }).start();*/

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 5, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadPoolExecutor.execute(()->{
                try {
                    Thread.sleep(200L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
        }
        /*if (threadPoolExecutor.getActiveCount() <= 0){
            threadPoolExecutor.shutdown();
        }*/


    }
}
